package com.zhangwei.liwushuo.fragment;


import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhangwei.liwushuo.R;
import com.zw.library.adapter.AAdapter;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

@EFragment(R.layout.common_ptr_layout)
public abstract class BasePtrFragment<T> extends BaseFragment implements PullToRefreshBase.OnRefreshListener2<ListView>, AdapterView.OnItemClickListener, View.OnClickListener {

    public List<T> list = new ArrayList<>();

    public AAdapter<T> adapter;

    @ViewById
    protected PullToRefreshListView commonRefreshListview;
    protected int mCurrentPage = 0;




    @Override
    @AfterViews
    public void initView() {
        ListView listView = commonRefreshListview.getRefreshableView();
        if (listView != null) {
            addHeadView(listView);
        }
        commonRefreshListview.setOnRefreshListener(this);
        commonRefreshListview.setOnItemClickListener(this);
        setAdapter(listView);

    }

    protected abstract void setAdapter(ListView listView);

    @Override
    public void initData() {
        loadData(commonRefreshListview, adapter, mCurrentPage);
    }

    public abstract void loadData(PullToRefreshListView ptrListView, AAdapter<T> adapter, int mCurrentPage);

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        mCurrentPage = 0;
        if (refreshView.getMode() != PullToRefreshBase.Mode.BOTH) {
            refreshView.setMode(PullToRefreshBase.Mode.BOTH);
        }
        loadData(commonRefreshListview, adapter, mCurrentPage);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        mCurrentPage++;
        loadData(commonRefreshListview, adapter, mCurrentPage);
    }

    public void addHeadView(ListView listView) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onClick(View v) {

    }

}
